package com.mattidroid.spiegel;

import java.util.Locale;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

public class applicationInit extends TabActivity implements OnTabChangeListener {
	
	static final int LANG_GER = 0;
	static final int LANG_ENG = 1;

	static final int TAB_ON = 1;	
	static final int TAB_OFF = 0;	
	
	static final int VISIBLE = 0;
	static final int INVISIBLE = 4;
	static final int GONE = 8;
	
	TabHost tabHost;	// Define Tabhost Object
	GlobalClass Data;	// Define Object for access to global data
	
    int toTabChange;
    int currentTab;
	   
    void GetBundleParameter() {
    	
    	// Point Object for access to global data to right context
    	GlobalClass GlobalData = ((GlobalClass)getApplicationContext());
    	currentTab = GlobalData.currentGlobalTab;
	    
		// Read bundle
    	Bundle bundle = this.getIntent().getExtras();
    	GlobalClass.log (false, "applicationInit GotBundle: " + bundle);  
    	// Catch non initialized Bundle at first start
		if (bundle == null) {
			GlobalClass.log (false, "applicationInit Bundle Param is NULL "); 
		    currentTab = 0;
		}
		else {
	    	toTabChange = bundle.getInt("page");
			GlobalClass.log (false, "applicationInit Change Tab by: " + toTabChange); 	
			// Change tab to Bundle Parameter received
			currentTab = currentTab + toTabChange;
			GlobalClass.log (false, "applicationInit Change to Tab: " + currentTab); 	
		}
		
		GlobalData.currentGlobalTab = currentTab;
 	
    }
	
	void getPrefsAndInitTabVariables(){
		
		// Point Object for access to global data to right context
		GlobalClass GlobalData = ((GlobalClass)getApplicationContext());
		// call GlobalClass getStoredSettings and read files from device: OK
		GlobalData.getStoredSettings();
	}
	
	// read locale settings from prefs and set accordingly
	void setLocale () {
		
		//Dynamic Override system settings LOCALE 
		//Need in all Classes
		GlobalClass GlobalData = ((GlobalClass)getApplicationContext());
	    String languageToLoad  = GlobalData.isLocaleString(GlobalData.overrideLocale);
	    GlobalClass.log(false, "applicationInit.setLocale Locale Setting to : " + languageToLoad);
	    
	    if (languageToLoad.equals("")) {
	    	// do nothing
	    	 GlobalClass.log(false, "applicationInit.setLocale Locale EMPTY");
	    }
	    else {
	    	//Override system settings LOCALE
		    Locale locale = new Locale(languageToLoad); 
		    Locale.setDefault(locale);
		    Configuration config = new Configuration();
		    config.locale = locale;
		    getBaseContext().getResources().updateConfiguration(config, 
		    getBaseContext().getResources().getDisplayMetrics());
	    }
	    
	}
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);

	    GlobalClass.log(false, "applicationInit.onCreate");
	    
	    // Point Object for access to global data to right context
		GlobalClass GlobalData = ((GlobalClass)getApplicationContext());

	    // Read Bundle and do something
	    GetBundleParameter();
	    
	    // GlobalClass GlobalData = ((GlobalClass)getApplicationContext());
	    getPrefsAndInitTabVariables();
		
	    // read locale settings from prefs and set accordingly
	    setLocale();
			
	    // spinner in header line : need to set here beforer tabhost is created
	    // requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
	    // setProgressBarIndeterminateVisibility(false);
	    
	    setContentView(R.layout.layout_main);

	    Resources res = getResources(); // Resource object to get Drawables
	    tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab

	    // colored tabs addon
        tabHost = (TabHost)findViewById(android.R.id.tabhost);
        tabHost.setOnTabChangedListener(this);
               
	    	// Start Tab ==========================================================
		    if (GlobalData._isStartTab == TAB_ON)
		    {
		    	GlobalClass.log(false,"applicationInit.onCreate StartTab");
		    	
		    	Bundle StartTab = new Bundle();
			    if (GlobalData._selectLanguage == LANG_GER)
			    	StartTab.putString("url", this.getString(R.string.start_url_de));	
			    else
			    	StartTab.putString("url", this.getString(R.string.start_url_en));			    
			    
		    	intent = new Intent().setClass(this, SingleWebView.class);   		
			    intent.putExtras(StartTab);    
			    TabHost.TabSpec StartTabSpec = tabHost.newTabSpec(this.getString(R.string.StartTab)).setIndicator(this.getString(R.string.StartTab),
			    				  res.getDrawable(R.drawable.tab_start))
		            .setContent(intent);    
			    tabHost.addTab(StartTabSpec);	
		    }
		    
		    // Wirtschaft Tab ==========================================================
		    if (GlobalData._isWirtschaftTab == TAB_ON)
		    {
		    	
		    	GlobalClass.log(false,"applicationInit.onCreate WirtschaftTab");
		    	
			    Bundle WirtschaftTab = new Bundle();
			    WirtschaftTab.putString("url", this.getString(R.string.wirtschaft_url_de));
	
		    	intent = new Intent().setClass(this, SingleWebView.class);   	
			    
			    intent.putExtras(WirtschaftTab);  
			    spec = tabHost.newTabSpec(this.getString(R.string.WirtschaftTab)).setIndicator(this.getString(R.string.WirtschaftTab),
		                res.getDrawable(R.drawable.tab_wirtschaft))
		            .setContent(intent);
			    tabHost.addTab(spec);
		    }

		    
		    // Politik Tab ==========================================================
		    if (GlobalData._isPolitikTab == TAB_ON)
		    {
		    	GlobalClass.log(false,"applicationInit.onCreate PolitikTab");
		    	
		    	Bundle PolitikTab = new Bundle();
			    PolitikTab.putString("url", this.getString(R.string.politik_url_de));
			    
		    	intent = new Intent().setClass(this, SingleWebView.class);   	
			    
			    intent.putExtras(PolitikTab);  
			    spec = tabHost.newTabSpec(this.getString(R.string.PolitikTab)).setIndicator(this.getString(R.string.PolitikTab),
			                      res.getDrawable(R.drawable.tab_politik))
			                  .setContent(intent);
			    tabHost.addTab(spec);
		    }
		    // Sport Tab ==========================================================
		    if (GlobalData._isSportTab == TAB_ON)
		    {		    
		    	GlobalClass.log(false,"applicationInit.onCreate SportTab");
		    	
		    	Bundle SportTab = new Bundle();
			    SportTab.putString("url", this.getString(R.string.sport_url_de));
			    
		    	intent = new Intent().setClass(this, SingleWebView.class);
			    
			    intent.putExtras(SportTab);  
			    spec = tabHost.newTabSpec(this.getString(R.string.SportTab)).setIndicator(this.getString(R.string.SportTab),   		
			                      res.getDrawable(R.drawable.tab_sport))
			                  .setContent(intent);
			    tabHost.addTab(spec);
		    }
		    // Netzwelt Tab ==========================================================
		    if (GlobalData._isNetzweltTab == TAB_ON)
		    {
		    	GlobalClass.log(false,"applicationInit.onCreate NetweltTab");
		    	
		    	Bundle NetweltTab = new Bundle();
			    NetweltTab.putString("url", this.getString(R.string.netzwelt_url_de));
			    
		    	intent = new Intent().setClass(this, SingleWebView.class);
			    
			    intent.putExtras(NetweltTab);  
			    spec = tabHost.newTabSpec(this.getString(R.string.NetzweltTab)).setIndicator(this.getString(R.string.NetzweltTab), 		
			                      res.getDrawable(R.drawable.tab_netzwelt))
			                  .setContent(intent);
			    tabHost.addTab(spec);
		    }
		    // Wissenschaft Tab ==========================================================
		    if (GlobalData._isWissenschaftTab == TAB_ON)
		    {
		    	GlobalClass.log(false,"applicationInit.onCreate WissenschaftTab");		    	
		    	
		    	Bundle WissenschaftTab = new Bundle();
			    WissenschaftTab.putString("url", this.getString(R.string.wissenschaft_url_de));
			    
		    	intent = new Intent().setClass(this, SingleWebView.class);
			    
			    intent.putExtras(WissenschaftTab);  
			    spec = tabHost.newTabSpec(this.getString(R.string.WissenschaftTab)).setIndicator(this.getString(R.string.WissenschaftTab), 		
			                      res.getDrawable(R.drawable.tab_wissenschaft))
			                  .setContent(intent);
			    tabHost.addTab(spec);
		    }

		    // Panorama Tab ==========================================================
		    if (GlobalData._isPanoramaTab == TAB_ON)
		    {
		    	GlobalClass.log(false,"applicationInit.onCreate PanoramaTab");	
		    	
			    Bundle PanoramaTab = new Bundle();
			    PanoramaTab.putString("url", this.getString(R.string.panorama_url_de));
			    
		    	intent = new Intent().setClass(this, SingleWebView.class);
			    
			    intent.putExtras(PanoramaTab);  
			    spec = tabHost.newTabSpec(this.getString(R.string.PanoramaTab)).setIndicator(this.getString(R.string.PanoramaTab), 		
			                      res.getDrawable(R.drawable.tab_panorama))
			                  .setContent(intent);
			    tabHost.addTab(spec);
		    }		    
		    // Ticker Tab ==========================================================
		    if (GlobalData._isTickerTab == TAB_ON)
		    {
		    	GlobalClass.log(false,"applicationInit.onCreate TickerTab");	
		    	
		    	Bundle TickerTab = new Bundle();
			    TickerTab.putString("url", this.getString(R.string.ticker_url_de));
			    
		    	intent = new Intent().setClass(this, SingleWebView.class);
			    
			    intent.putExtras(TickerTab);  
			    spec = tabHost.newTabSpec(this.getString(R.string.TickerTab)).setIndicator(this.getString(R.string.TickerTab), 		
			                      res.getDrawable(R.drawable.tab_ticker))
			                  .setContent(intent);
			    tabHost.addTab(spec);
		    }		    
		    // Rubriken Tab ==========================================================
		    if (GlobalData._isRubrikenTab == TAB_ON)
		    {
		    	GlobalClass.log(false,"applicationInit.onCreate RubrikenTab");	
		    	
		    	Bundle RubrikenTab = new Bundle();
			    RubrikenTab.putString("url", this.getString(R.string.rubriken_url_de));
			    
		    	intent = new Intent().setClass(this, SingleWebView.class);
			    
			    intent.putExtras(RubrikenTab);  
			    spec = tabHost.newTabSpec(this.getString(R.string.RubrikenTab)).setIndicator(this.getString(R.string.RubrikenTab), 		
			                      res.getDrawable(R.drawable.tab_rubriken))
			                  .setContent(intent);
			    tabHost.addTab(spec);
		    }		    
		    // Ende Tab ==========================================================

		    // color tab initially
	        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
	        {
	        	tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor(this.getString(R.color.colorBlack)));
	        } 	

	        // set initial active colored tab
	        tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor(this.getString(R.color.colorSpiegelRed))); 
	        
		    // Set tab to "current" tab        
	        GlobalClass.log (false, "applicationInit.onCreate currentTab: " + currentTab);
	        tabHost.setCurrentTab(currentTab);

	}
		
	// color tab on change
	@Override
	public void onTabChanged(String tabId) { 
		
		// pointer to globalClass
		GlobalClass GlobalData = ((GlobalClass)getApplicationContext());
		
		// Store current Tab
		GlobalData.currentGlobalTab = tabHost.getCurrentTab();
		GlobalClass.log (false, "applicationInit onTabChanged: " + tabId + " : " + GlobalData.currentGlobalTab);
		
		for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
        {
        	tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor(this.getString(R.color.colorBlack)));
        } 
				
		tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor(this.getString(R.color.colorSpiegelRed)));
	} 	
	
	public int changeTabTo (int TabNumber) {
		tabHost.setCurrentTab(TabNumber);
		return tabHost.getCurrentTab();
	}
}

